using System;
using System.Collections.Generic;

namespace MetaObjects.Core
{
	public static class EnumExtensions
	{
		public static T AsEnum<T>(this string value)
		{
			return AsEnum<T>(value, false);
		}

		public static T AsEnum<T>(this string value, bool ignoreCase)
		{
			if (string.IsNullOrEmpty(value))
			{
				throw new ArgumentNullException("value", "Can't parse an empty string");
			}
			Type enumType = typeof(T);
			if (!enumType.IsEnum)
			{
				throw new InvalidOperationException("Type must be System.Enum.");
			}
			// warning, can throw
			return (T)Enum.Parse(enumType, value, ignoreCase);
		}
		
		public static IEnumerable<T> GetValues<T>()
		{
			foreach (object value in Enum.GetValues(typeof(T)))
			{
				yield return (T) value;
			}
		}
	}
}